using System;
using System.Collections.Generic;
using System.Text;
using Ent.Voice.Show;
using System.Data;
using Ent.SqlDB;

namespace Ent.Voice.Nomination
{
    public enum NominationStatus { NotAllowed, NotStarted, DueDateOver, MaxCountOver, InsufficientCredit, Allowed, Nominated }
    public enum NominationResponse { Error, NominationCreated, NominationUpdated, NominationNotFound }
    public struct UserNomination
    {
        public NominationStatus Status;
        public List<EventFileType> FileTypes;
        public int EntryId;
        public UserNomination(NominationStatus nomStatus, List<EventFileType> fTypes, int enId)
        {
            Status = nomStatus;
            FileTypes = fTypes;
            EntryId = enId;
        }
    }
    public class NominationProperty
    {
        public const string EntryId = "EntryId";
        public const string EventId = "EventId";
        public const string Title = "Title";
        public const string Description = "Description";
        public const string Type = "Type";
        public const string FileName = "FileName";
        public const string TextContent = "TextContent";
        public const string Height = "Height";
        public const string Width = "Width";
    }
    public class Nomination
    {
        public int EntryId;
        public string UserId;
        public int EventId = -1;
        public string Title;
        public string Description;
        public EventFileType Type;
        public string FileName;
        public string TextContent;
        public int Height;
        public int Width;
        public string Invitees;
        public static UserNomination GetUserNomination(int eventid, string userid)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("EventId", eventid);
            pars.Add("UserId", userid);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("nomstatus", new VSqlDbType(SqlDbType.VarChar, 20));
            outpars.Add("nomId", new VSqlDbType(SqlDbType.Int));
            ReaderResponse rr = Execute.SPReader("Voice_GetUserNomination", pars, outpars);
            NominationStatus status = (NominationStatus)Enum.Parse(typeof(NominationStatus), rr.OutParams["nomstatus"].ToString());
            int entryId = (int)rr.OutParams["nomId"];
            List<EventFileType> list = new List<EventFileType>();
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    list.Add((EventFileType)Enum.Parse(typeof(EventFileType), rr.Table.Rows[i][0].ToString()));
                }
            }

            return new UserNomination(status, list, entryId);
        }
        public static NominationResponse Create(Nomination nData)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("evId", nData.EventId);
            pars.Add("userid", nData.UserId);
            pars.Add("title", nData.Title);
            pars.Add("description", nData.Description);
            pars.Add("type", nData.Type.ToString());
            pars.Add("filePath", nData.FileName);
            pars.Add("text", nData.TextContent);
            pars.Add("height", nData.Height);
            pars.Add("width", nData.Width);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("EntryId", new VSqlDbType(SqlDbType.Int));
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_SetNomination", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            nData.EntryId = Convert.ToInt32(nqr.OutParams["EntryId"]);
            switch (res.ToLower())
            {
                case "nominationcreated":
                    return NominationResponse.NominationCreated;
                default:
                    return NominationResponse.Error;
            }
        }
        public static NominationResponse Update(Nomination nData)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("EntryId", nData.EntryId);
            pars.Add("title", nData.Title);
            pars.Add("description", nData.Description);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_UpdateNomination", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            switch (res.ToLower())
            {
                case "nominationupdated":
                    return NominationResponse.NominationUpdated;
                case "nominationnotfound":
                    return NominationResponse.NominationNotFound;
                default:
                    return NominationResponse.Error;
            }
        }
    }
}
